isEmpty (PROTOS) {
    error (Proto files is not defined in PROTOS!)
}

isEmpty (PROTODIR) {
    error (Proto dir files is not defined in PROTODIR!)
}

DEPENDPATH += $$PROTODIR

PROTOBUF_INCLUDE = $$(PROTOBUF_INCLUDE)
isEmpty(PROTOBUF_INCLUDE) {
    PROTOBUF_INCLUDE = $$system(pkg-config --variable=includedir protobuf)
}
PROTOBUF_LIBS = $$(PROTOBUF_LIBS)
isEmpty(PROTOBUF_LIBS) {
    PROTOBUF_LIBS = $$system(pkg-config --libs protobuf)
}

protobuf_decl.name = protobuf header
protobuf_decl.input = PROTOS
protobuf_decl.output = $${PROTOSUBDIR}/${QMAKE_FILE_BASE}.pb.h
protobuf_decl.commands = protoc --cpp_out=$${OUT_PWD} ${QMAKE_FILE_NAME} \
        --proto_path=$$PROTOBUF_INCLUDE \
        --proto_path=$(patsubst %/$$PROTOSUBDIR,%,${QMAKE_FILE_PATH}) \
        $$ADDITIONAL_PROTO_PATHS
protobuf_decl.variable_out = GENERATED_FILES
QMAKE_EXTRA_COMPILERS += protobuf_decl

protobuf_impl.name = protobuf implementation
protobuf_impl.input = PROTOS
protobuf_impl.output = $${PROTOSUBDIR}/${QMAKE_FILE_BASE}.pb.cc
protobuf_impl.depends = $${PROTOSUBDIR}/${QMAKE_FILE_BASE}.pb.h
protobuf_impl.commands = $$escape_expand("\\n")
protobuf_impl.variable_out = GENERATED_SOURCES
QMAKE_EXTRA_COMPILERS += protobuf_impl

#target.depends = protobuf_impl

INCLUDEPATH *= $$OUT_PWD $$PROTOBUF_INCLUDE
LIBS += $$PROTOBUF_LIBS -Wl,--no-undefined
